package com.longjianghuasheng.logistics.business.main

import android.os.Bundle
import androidx.core.view.isGone
import androidx.core.view.isVisible
import com.longjianghuasheng.logistics.R
import com.longjianghuasheng.logistics.base.BaseFragment
import com.longjianghuasheng.logistics.databinding.FragmentMain2Binding
import com.longjianghuasheng.logistics.eventViewModel
import com.longjianghuasheng.logistics.ext.initMain
import com.longjianghuasheng.logistics.util.CacheUtil
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel

class Main2Fragment : BaseFragment<BaseViewModel, FragmentMain2Binding>() {
    override fun layoutId(): Int = R.layout.fragment_main2

    override fun initView(savedInstanceState: Bundle?) {


        mDatabind.init()

    }

    private fun FragmentMain2Binding.init() {


        mainViewpager.initMain(this@Main2Fragment)

        radioGroup.setOnCheckedChangeListener { _, checkedId ->
            when (checkedId) {
                R.id.rb_home -> mainViewpager.setCurrentItem(0, false)

                R.id.rb_grab_an_order -> mainViewpager.setCurrentItem(1, false)

                R.id.rb_consult -> mainViewpager.setCurrentItem(2, false)

                R.id.rb_mine -> mainViewpager.setCurrentItem(3, false)

            }

        }


        switchUser()

        ivCenter.setOnClickListener {

            radioGroup.clearCheck()
            mainViewpager.setCurrentItem(1, false)
        }

        radioGroup.check(R.id.rb_home)

        circleButton.isGone = CacheUtil.isDriver

        circleButton.setOnClickListener { navigate(R.id.askForAQuotationFragment) }
    }


    override fun createObserver() {
        eventViewModel.loginEvent.observeInFragment(this) {
//            mDatabind.switchUser()
            mDatabind.init()
        }
        eventViewModel.toFreightEvent.observeInFragment(this) {

            mDatabind.radioGroup.check(if (CacheUtil.isDriver) R.id.rb_home else R.id.rb_grab_an_order)
        }
    }

    private fun FragmentMain2Binding.switchUser() {
        viewCenter.isGone = CacheUtil.isDriver
        ivCenter.isGone = CacheUtil.isDriver

        rbGrabAnOrder.isVisible = CacheUtil.isDriver
        rbConsult.isVisible = CacheUtil.isDriver
    }

}